package com.byxy.xyqas.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.byxy.xyqas.dao.ForumDao;
import com.byxy.xyqas.dao.TabDao;
import com.byxy.xyqas.entity.Forum;
import com.byxy.xyqas.entity.Tab;
import com.byxy.xyqas.service.TabService;
import com.byxy.xyqas.vo.TabVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class TabServiceImpl implements TabService {
	@Autowired
	private TabDao tabDao;

	@Autowired
	private ForumDao forumDao;

	public List<TabVO> findAll() {
		List<TabVO> tabVOs = new ArrayList<>();
		List<Tab> tabs = tabDao.findAll();
		for (Tab tab : tabs) {
			TabVO tabVO = new TabVO();
			BeanUtils.copyProperties(tab, tabVO);
			Forum f = forumDao.getOne(tab.getForumId());
			tabVO.setForumName(f.getForumName());
			tabVOs.add(tabVO);
		}
		return tabVOs;
	}

	public TabVO get(Integer tabId) {
		Tab tab = tabDao.getOne(tabId);
		TabVO tabVO = new TabVO();
		BeanUtils.copyProperties(tab, tabVO);
		return tabVO;
	}

	public void delete(Integer tabId) {
		tabDao.deleteById(tabId);
	}

	@Override
	public void update(TabVO tabVO) {
		Tab tab = new Tab();
		BeanUtils.copyProperties(tabVO, tab);
		tabDao.save(tab);
	}

	@Override
	public void add(TabVO tabVO) {
		Tab tab = new Tab();
		BeanUtils.copyProperties(tabVO, tab);
		tabDao.save(tab);
	}

}
